package cn.haoma.base.common;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.lang.reflect.Field;

import cn.haoma.base.R;
import cn.haoma.base.utils.ToastUtil;


public class BaseFragment extends Fragment implements View.OnClickListener {
    protected View rootView;

    protected void cancelLoadingDialog() {
        BaseActivity baseActivity = (BaseActivity) getActivity();
        if (baseActivity != null) {
            baseActivity.cancelLoadingDialog();
        }
    }

    public <T extends View> T f(int id, View v) {
        return (T) v.findViewById(id);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * setGone
     * 设置View[]为不可见gone
     *
     * @param views
     */
    public void sg(View... views) {
        for (View v : views) {
            v.setVisibility(View.GONE);
        }
    }

    protected void showLoadingDialog() {
        BaseActivity baseActivity = (BaseActivity) getActivity();
        if (baseActivity != null) {
            baseActivity.showLoadingDialog();
        }
    }

    protected void showToast(Object obj) {
        ToastUtil.showShortToast(getActivity(), obj);
    }

    /**
     * setInvisiable
     * 设置View[]为不可见invisiable
     *
     * @param views
     */
    public void si(View... views) {
        for (View v : views) {
            v.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public void startActivityForResult(Intent intent, int requestCode) {
        super.startActivityForResult(intent, requestCode);
        getActivity().overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        loadView(inflater, container);
        return rootView;
    }

    public void loadView(LayoutInflater layoutInflater, ViewGroup viewGroup) {
        if (getClass().isAnnotationPresent(LayoutId.class)) {
            rootView = layoutInflater.inflate(getClass().getAnnotation(LayoutId.class).value(), viewGroup, false);
            Field[] fields = getClass().getDeclaredFields();
            try {
                for (Field field : fields) {
                    if (field.isAnnotationPresent(Id.class)) {
                        field.setAccessible(true);
                        field.set(this, rootView.findViewById(field.getAnnotation(Id.class).value()));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
                e.getStackTrace();
                Log.e(BaseActivity.class.getName(), e.toString());
            } catch (Error ee) {

            }
        }
    }

    /**
     * setVisiable
     * 设置View[]为可见visiable
     *
     * @param views
     */
    public void sv(View... views) {
        for (View v : views) {
            v.setVisibility(View.VISIBLE);
        }
    }
}
